﻿Imports ADOEntity
Imports Microsoft.VisualBasic
Imports System.Globalization

Public Class clsDateFormat

    Dim config As systemConfigEntities = New systemConfigEntities

    Enum enmAutoDefaultDate
        pwdExpire = 0
        userExpire = 1
        userLock = 2

    End Enum

    Public Enum enmDateCultureInfo
        En = 0
        Th = 1
    End Enum


    'Public Function setFormatDate(ByVal strDate As String) As String

    '    Dim strDateReturn As String = ""
    '    Dim DateToFormat As Date = Nothing
    '    Try
    '        DateToFormat = strDate
    '        strDateReturn = DateToFormat.ToString("dd/MM/yyyy", New System.Globalization.CultureInfo("th-TH"))
    '    Catch ex As Exception
    '        Throw ex


    '    End Try
    '    Return strDateReturn

    'End Function

    'Public Function setFormatDate() As String
    '    Dim strDateReturn As String = ""

    '    Try
    '        strDateReturn = DateTime.Now.ToString("dd/MM/yyyy", New System.Globalization.CultureInfo("th-TH"))
    '    Catch ex As Exception
    '        Throw ex
    '        'strErrExp = ex.Message
    '    End Try
    '    Return strDateReturn
    'End Function

    Public Function AutoDate(ByVal enmReturnDate As enmAutoDefaultDate) As String
        Dim dteReturn As Date


        Dim qryConfig = From tblConfig In config.SSYSTEMCONFIG
        If (enmReturnDate = enmAutoDefaultDate.pwdExpire) Then
            Dim PwdExpire = (qryConfig.Where(Function(f) f.CONFIGCODE.Contains("C01"))).ToList
            dteReturn = Date.Now.AddDays(PwdExpire(0).CONFIGNUM)

        ElseIf (enmReturnDate = enmAutoDefaultDate.userExpire) Then
            Dim UserExpire = (qryConfig.Where(Function(f) f.CONFIGCODE.Contains("C06"))).ToList
            dteReturn = Date.Now.AddDays(UserExpire(0).CONFIGNUM)

        End If

        Return chkDateThOrEn(enmDateCultureInfo.Th, dteDateIsChk:=dteReturn)
    End Function

    Public Function AutoDate(ByVal enmReturnDate As enmAutoDefaultDate, ByVal enmCultureDateToUse As enmDateCultureInfo, Optional ByVal strRoleCode As String = "") As String
        Dim dteReturn As Date

        Dim qryConfig = From tblConfig In config.SSYSTEMCONFIG
        If (enmReturnDate = enmAutoDefaultDate.pwdExpire) Then
            If Not (strRoleCode = "") Then
                If Convert.ToInt32(strRoleCode) < 50 Then
                    Dim PwdExpire = (qryConfig.Where(Function(f) f.CONFIGCODE.Contains("C07"))).ToList
                    dteReturn = Date.Now.AddDays(PwdExpire(0).CONFIGNUM)
                Else
                    Dim PwdExpire = (qryConfig.Where(Function(f) f.CONFIGCODE.Contains("C01"))).ToList
                    dteReturn = Date.Now.AddDays(PwdExpire(0).CONFIGNUM)
                End If

            Else
                Dim PwdExpire = (qryConfig.Where(Function(f) f.CONFIGCODE.Contains("C01"))).ToList
                dteReturn = Date.Now.AddDays(PwdExpire(0).CONFIGNUM)
            End If


        ElseIf (enmReturnDate = enmAutoDefaultDate.userExpire) Then
            Dim UserExpire = (qryConfig.Where(Function(f) f.CONFIGCODE.Contains("C06"))).ToList
            dteReturn = Date.Now.AddDays(UserExpire(0).CONFIGNUM)

        End If

        Return chkDateThOrEn(enmCultureDateToUse, dteDateIsChk:=dteReturn)
    End Function

    Private Function SetDateDiffYear(ByVal strDate As String) As String
        Dim strReturnSave As String = ""
        Dim dteDateSave As Date = Nothing
        Try

            If Not (strDate = "") Then
                dteDateSave = strDate
                'strReturnSave = FormatDateTime(dteDateSave, DateFormat.ShortDate)
                strReturnSave = FormatDateTime(dteDateSave.AddYears(-543), DateFormat.ShortDate)
            End If
            Return strReturnSave

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function SetDatePrositiveYear(ByVal strDate As String) As String
        Dim resultdate As String = ""
        If Not (strDate = "") Then
            Dim convertdate As DateTime = DateTime.Parse(strDate)
            resultdate = FormatDateTime(convertdate.AddYears(543), DateFormat.ShortDate)
            ' resultdate = FormatDateTime(convertdate, DateFormat.ShortDate)
        End If
        Return resultdate
    End Function

    Private Function SetDateTHBeforeSave(ByVal strDate As String) As String
        Dim resultdate As String = ""
        If Not (strDate = "") Then
            Dim convertdate As DateTime = DateTime.Parse(strDate)
            resultdate = FormatDateTime(convertdate.AddYears(-543), DateFormat.ShortDate)
            ' resultdate = FormatDateTime(convertdate, DateFormat.ShortDate)
        End If
        Return resultdate

    End Function

    Public Function chkDateThOrEn(ByVal enmCultureDateToUse As enmDateCultureInfo, _
                                   Optional ByVal strDateIsChk As String = "", _
                                   Optional ByVal dteDateIsChk As Date = Nothing) As String

        Dim dteConvert As String = ""
        Dim subStrDate() As String = Nothing
        Dim chkstrIsDate As Date = Nothing
        Dim intYear As Integer = 0
        Dim yearInServer As Integer = 0
        'เอาปีจากเครื่อง server
        yearInServer = CultureInfo.CurrentCulture.Calendar.GetYear(Date.Now)


        Try
            'เช็คว่า ตัวแปร วันที่เป็นค่าว่างหรือ nothing หรือไม่
            If Not strDateIsChk = "" Then

                chkstrIsDate = strDateIsChk

            ElseIf Not dteDateIsChk = Nothing Then
                chkstrIsDate = dteDateIsChk
            End If
            ' เป็นการ convert วันที่เป็น วันเดือนปี แล้วก็ split ค่าออก
            subStrDate = chkstrIsDate.ToString("dd/MM/yyyy").Split("/")
            'subStrDate = chkstrIsDate.ToString.Split("/")

            ' เอาค่าที slpit เอาเฉพาะ ปีมาเช็คเงือนไข
            intYear = subStrDate(2)

            If (intYear > 2400) Then 'ปีไทย ปีที่เข้ามาเป็นปีไทย

                If (enmCultureDateToUse = enmDateCultureInfo.En) Then 'เช็คว่าต้องการวันที่ อังกฤษ

                    If (yearInServer > 2400) Then 'เป็นปีไทย
                        dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + (Convert.ToInt32(subStrDate(2))).ToString
                    ElseIf (yearInServer <= 2400) Then 'เป็นปีอังกฤษ
                        dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + (Convert.ToInt32(subStrDate(2)) - 543).ToString
                    End If

                ElseIf (enmCultureDateToUse = enmDateCultureInfo.Th) Then 'เช็คว่าต้องการวันที่ ไทย

                    dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + subStrDate(2)

                End If
            ElseIf (intYear <= 2400) Then 'ปีอังกฤษ ปีที่เข้ามาเป็นปีอังกฤษ

                If (enmCultureDateToUse = enmDateCultureInfo.En) Then 'เช็คว่าต้องการวันที่ อังกฤษ

                    dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + subStrDate(2)

                ElseIf (enmCultureDateToUse = enmDateCultureInfo.Th) Then 'เช็คว่าต้องการวันที่ ไทย

                    If (yearInServer > 2400) Then 'เป็นปีไทย
                        dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + (Convert.ToInt32(subStrDate(2)) + 543).ToString
                    ElseIf (yearInServer <= 2400) Then 'เป็นปีอังกฤษ
                        dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + (Convert.ToInt32(subStrDate(2))).ToString
                    End If

                End If

            End If

        Catch ex As FormatException

        Catch ex As Exception

        End Try

        Return dteConvert
    End Function

    Public Function GetYearLast(ByVal enmCultureDateToUse As enmDateCultureInfo) As String
        ' Dim strYear As String = ""
        Dim intConvertYear As Integer = 0
        Dim intYear As Integer = Date.Now.Year

        If (intYear > 2500) Then 'ปีไทย
            If (enmCultureDateToUse = enmDateCultureInfo.En) Then
                intConvertYear = intYear - 543
            ElseIf (enmCultureDateToUse = enmDateCultureInfo.Th) Then
                intConvertYear = intYear
            End If
        ElseIf (intYear <= 2500) Then 'ปีอังกฤษ
            If (enmCultureDateToUse = enmDateCultureInfo.En) Then
                intConvertYear = intYear
            ElseIf (enmCultureDateToUse = enmDateCultureInfo.Th) Then
                intConvertYear = intYear + 543
            End If
        End If
        Return intConvertYear
    End Function

    Public Function convertIntToDate(ByVal enmCultureDateToUse As enmDateCultureInfo, ByVal intDate As String) As String
        Dim intChkString As Integer = 0
        Dim strDate As String
        Try
            intChkString = intDate ' เป็นการเช็คว่าเป็น int จริงหรือเปลช่ร
            strDate = intDate.ToString.Insert(2, "/").Insert(5, "/") '
            Dim chkDateFormat As Date = Nothing
            chkDateFormat = strDate

            strDate = chkDateThOrEn(enmCultureDateToUse, chkDateFormat)
        Catch ex As Exception
            Throw ex
        End Try
        Return strDate
    End Function

    Public Function convertDateToInt(ByVal enmCultureDateToUse As enmDateCultureInfo, Optional ByVal strDate As String = "", Optional ByVal dteDate As Date = Nothing) As String
        Try
            Dim chkDateFormat As Date = Nothing
            If Not (strDate = "") Then
                chkDateFormat = strDate
                strDate = chkDateThOrEn(enmCultureDateToUse, chkDateFormat)
                strDate = strDate.ToString.Replace("/", "")
            ElseIf Not (dteDate = Nothing) Then
                strDate = chkDateThOrEn(enmCultureDateToUse, dteDate)
                strDate = strDate.ToString.Replace("/", "")
            End If

        Catch ex As Exception
            Throw ex
        End Try

        Return strDate
    End Function


    Public Function ChkConvertIntToDate(ByVal enmCultureDateToUse As enmDateCultureInfo, ByVal intDate As String) As Boolean
        Dim intChkString As Integer = 0
        Dim blnChkDate As Boolean = False
        Dim strDate As String
        Try
            intChkString = intDate ' เป็นการเช็คว่าเป็น int จริงหรือเปลช่ร
            strDate = intDate.ToString.Insert(2, "/").Insert(5, "/") '
            Dim chkDateFormat As Date = Nothing
            chkDateFormat = strDate
            blnChkDate = True
            '    strDate = chkDateThOrEn(enmDateCultureInfo.Th, chkDateFormat)
        Catch ex As Exception
            ' Throw ex
            blnChkDate = False
        End Try
        Return blnChkDate
    End Function

    Public Function ChkformatDate(ByVal strDate As String) As Boolean
        Dim blnChkFormatDate As Boolean = True
        Try
            Dim dteChk As Date = strDate
            blnChkFormatDate = True
        Catch ex As Exception
            blnChkFormatDate = False
        End Try

        Return blnChkFormatDate
    End Function

    Public Function chkFormatString(ByVal strText As String) As Boolean
        Dim blnChkFormatInteger As Boolean = True
        Try
            Dim intChk As Integer = strText
            blnChkFormatInteger = False
        Catch ex As Exception
            blnChkFormatInteger = True
        End Try

        Return blnChkFormatInteger

    End Function

    Public Function chkStartDateEndDate(ByVal startDate As Date, ByVal endDate As Date) As Boolean
        Dim valid As Boolean = True
        If startDate > endDate Then
            valid = False
        End If
        Return valid
    End Function

    Public Function chkDateTimeThOrEn(ByVal enmCultureDateToUse As enmDateCultureInfo, _
                                   Optional ByVal strDateIsChk As String = "", _
                                   Optional ByVal dteDateIsChk As Date = Nothing) As String

        Dim dteConvert As String = ""
        Dim subStrDate() As String = Nothing
        Dim subStrTime As String = Nothing
        Dim chkstrIsDate As Date = Nothing
        Dim intYear As Integer = 0
        Dim yearInServer As Integer = 0
        'เอาปีจากเครื่อง server
        yearInServer = CultureInfo.CurrentCulture.Calendar.GetYear(Date.Now)


        Try
            'เช็คว่า ตัวแปร วันที่เป็นค่าว่างหรือ nothing หรือไม่
            If Not strDateIsChk = "" Then

                chkstrIsDate = strDateIsChk

            ElseIf Not dteDateIsChk = Nothing Then
                chkstrIsDate = dteDateIsChk
            End If
            ' เป็นการ convert วันที่เป็น วันเดือนปี แล้วก็ split ค่าออก
            subStrDate = chkstrIsDate.ToString("dd/MM/yyyy").Split("/")
            subStrTime = chkstrIsDate.ToString("HH:mm:ss")

            'subStrDate = chkstrIsDate.ToString.Split("/")

            ' เอาค่าที slpit เอาเฉพาะ ปีมาเช็คเงือนไข
            intYear = subStrDate(2)

            If (intYear > 2400) Then 'ปีไทย ปีที่เข้ามาเป็นปีไทย

                If (enmCultureDateToUse = enmDateCultureInfo.En) Then 'เช็คว่าต้องการวันที่ อังกฤษ

                    If (yearInServer > 2400) Then 'เป็นปีไทย
                        dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + (Convert.ToInt32(subStrDate(2))).ToString + " " + subStrTime
                    ElseIf (yearInServer <= 2400) Then 'เป็นปีอังกฤษ
                        dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + (Convert.ToInt32(subStrDate(2)) - 543).ToString + " " + subStrTime
                    End If

                ElseIf (enmCultureDateToUse = enmDateCultureInfo.Th) Then 'เช็คว่าต้องการวันที่ ไทย

                    dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + subStrDate(2) + " " + subStrTime

                End If
            ElseIf (intYear <= 2400) Then 'ปีอังกฤษ ปีที่เข้ามาเป็นปีอังกฤษ

                If (enmCultureDateToUse = enmDateCultureInfo.En) Then 'เช็คว่าต้องการวันที่ อังกฤษ

                    dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + subStrDate(2) + " " + subStrTime

                ElseIf (enmCultureDateToUse = enmDateCultureInfo.Th) Then 'เช็คว่าต้องการวันที่ ไทย

                    If (yearInServer > 2400) Then 'เป็นปีไทย
                        dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + (Convert.ToInt32(subStrDate(2)) + 543).ToString + " " + subStrTime
                    ElseIf (yearInServer <= 2400) Then 'เป็นปีอังกฤษ
                        dteConvert = subStrDate(0) + "/" + subStrDate(1) + "/" + (Convert.ToInt32(subStrDate(2))).ToString + " " + subStrTime
                    End If

                End If

            End If

        Catch ex As FormatException

        Catch ex As Exception

        End Try

        Return dteConvert
    End Function
End Class
